#include <soar.h>

int main(int argc, char *argv[])
{
    int res;
    unsigned int algm;

    if(argc !=3 && argc != 4)
    {
        printf("Usage: decode_file algorithm(BASE64) "
                "enc_file [plain_file]\n");
        return(-1);
    }
    if(strcasecmp(argv[1], "BASE64") == 0)
        algm=OSSL_ENCODE_BASE64;
    else
    {
        printf("算法(%s)不支持\n", argv[1]);
        return(-1);
    }

    ossl_init();

    if(argc == 3)
    {
        char *buf;
        struct stat sb;
        int size;
        if(stat(argv[2], &sb))
        {
            printf("enc_file(%s)无效\n", argv[2]);
            return(-1);
        }
        ALLOC_MULTI_DOERR(buf, char, sb.st_size+1,
                printf("内存分配出错\n"); return(-1));
        if((res=ossl_decode_file(algm, argv[2],
                        (unsigned char *)buf, &size)))
        {
            printf("ossl_decode_file出错, res:%d\n", res);
            return(-1);
        }
        printf("%s", buf);
    }
    else
    {
        if((res=ossl_decode_file_to_file(algm, argv[2], argv[3])))
        {
            printf("ossl_decode_file_to_file出错, res:%d\n", res);
            return(-1);
        }
    }

    ossl_clean();

    return(0);
}
